import * as VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import personalChildren from './personal.js'
import VueCookies from 'vue-cookies'

const {
	createRouter,
	createWebHashHistory
} = VueRouter;
// console.log(VueRouter)

const routes = [
	{
		path: '/',
		name: 'Home',
		component: Home
	},
	{
		path: '/personal',
		name: 'Personal',
		component: () => import('../views/Personal.vue'),
		children:personalChildren
	},
	{
		path: '/register',
		name: 'Register',
		component: () => import('../views/Register.vue')
	},
	{
		path: '/login',
		name: 'Login',
		component: () => import('../views/Login.vue')
	}
]

const router = createRouter({
	history: createWebHashHistory(process.env.BASE_URL),
	routes
})

router.beforeEach((to,from,next)=>{
	if(to.name == 'Login' || to.name == 'Register'){
		next()
	}else{
		if(VueCookies.isKey('Authorization')){
			next()
		}else{
			next({
				name:'Login'
			})
		}
	}
})

export default router
